<template>
  <div class="login-account">
    <el-form label-width="60px" :model="account" :rules="rules" ref="formRef">
      <el-form-item label="账号:" prop="userName">
        <el-input v-model="account.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="account.password"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { ElForm } from 'element-plus'
import { defineComponent, reactive, ref } from 'vue'
import { rules } from '../config/account-config'
import dataCache from '@/utils/cache'

export default defineComponent({
  setup() {
    const account = reactive({
      userName: dataCache.getCache('userName') ?? '',
      password: dataCache.getCache('password') ?? ''
    })

    const formRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      formRef.value?.validate((valid) => {
        // 1.判断是否需要记住密码
        if (isKeepPassword === true) {
          // 2.1.把账号信息存储在缓存中
          dataCache.setCache('userName', account.userName)
          dataCache.setCache('password', account.password)
        } else {
          // 2.2.把账号信息消除
          dataCache.deleteCache('userName', account.userName)
          dataCache.deleteCache('password', account.password)
        }
      })
    }

    return {
      account,
      rules,
      loginAction,
      formRef
    }
  }
})
</script>

<style scoped></style>
